/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.binding.components.renderers;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import ch.trackedbean.binding.mapping.*;
import ch.trackedbean.tracking.*;

/**
 * DefaultCellRenderer for "TrackedBean" tables.<br>
 * Changes the backgroundcolor according to the property state.
 * 
 * @author M. Hautle
 */
public class DefaultPropertyCellRenderer extends DefaultTableCellRenderer {
    // TODO get colors from a central location which should be configurable (static setters?)
    /** Color indicating an technical error. */
    private static final Color TECHNICAL_ERROR = Color.RED.darker();

    /** Color indicating an error. */
    private static final Color ERROR = Color.RED;

    /** Color indicating an changed value. */
    private static final Color CHANGED = new Color(220, 220, 255);

    /** The tabelmapping owning this renderer */
    private final TableMapping mapping;

    /** Flag indicating if the backgroundcolor should be adapted according to the status of the represented cell. */
    private boolean showStatus;

    /**
     * Default constructor
     * 
     * @param showStatus True if the background should be adapted according to the cell state
     * @param mapping The tablemapping owning this renderer
     */
    public DefaultPropertyCellRenderer(final TableMapping mapping, boolean showStatus) {
        this.mapping = mapping;
        this.showStatus = showStatus;
    }

    /**
     * Flag indicating if the backgroundcolor should be adapted according to the status of the represented cell.
     * 
     * @return True if the background gets adapted.
     */
    public boolean isShowStatus() {
        return showStatus;
    }

    /**
     * Flag indicating if the backgroundcolor should be adapted according to the status of the represented cell.
     * 
     * @param showStatus True to adapt the background.
     */
    public void setShowStatus(boolean showStatus) {
        this.showStatus = showStatus;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row,
            final int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if (isSelected || !showStatus)
            return this;

        final Status status = mapping.getCellStatus(row, column);
        final BeanStatusFlag flag = status != null ? status.getFlag() : BeanStatusFlag.UNCHANGED;
        switch (flag) {
            case CHANGED:
                setBackground(CHANGED);
                break;
            case ERROR:
                setBackground(ERROR);
                break;
            case TECHNICAL_ERROR:
                setBackground(TECHNICAL_ERROR);
                break;
            default:
                setBackground(table.getBackground());
        }
        return this;
    }
}
